package com.yequan.leetcode.tree.treedfs;

import com.yequan.leetcode.tree.TreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author : Administrator
 * @date : 2020/5/2
 */
public class RecodeTreeDfs {

    class WithStack {
        /**
         * 使用栈实现dfs
         */
        public List<Integer> dfs1(TreeNode root) {
            List<Integer> ans = new ArrayList<>();
            Stack<TreeNode> stack = new Stack<>();
            stack.push(root);
            while (!stack.isEmpty()) {
                TreeNode node = stack.pop();
                ans.add(node.val);
                if (null != node.right) {
                    stack.push(node.right);
                }
                if (null != node.left) {
                    stack.push(node.left);
                }
            }
            return ans;
        }

        public List<Integer> dfs2(TreeNode root) {
            List<Integer> res = new ArrayList<>();
            Stack<TreeNode> stack = new Stack<>();
            stack.push(root);
            while (!stack.isEmpty()) {
                TreeNode node = stack.pop();
                res.add(node.val);
                if (null != node.right) {
                    stack.push(node.right);
                }
                if (null != node.left) {
                    stack.push(node.left);
                }
            }
            return res;
        }

        public List<Integer> dfs3(TreeNode root) {
            if (null == root) {
                return null;
            }
            List<Integer> res = new ArrayList<>();
            Stack<TreeNode> stack = new Stack<>();
            stack.push(root);
            while (!stack.isEmpty()) {
                TreeNode node = stack.pop();
                res.add(node.val);
                if (null != node.right) {
                    stack.push(node.right);
                }
                if (null != node.left) {
                    stack.push(node.left);
                }
            }
            return res;
        }

        public List<Integer> dfs4(TreeNode root) {
            List<Integer> res = new ArrayList<>();
            Stack<TreeNode> stack = new Stack<>();
            stack.push(root);
            while (!stack.isEmpty()) {
                TreeNode currentNode = stack.pop();
                res.add(currentNode.val);
                if (currentNode.right != null) {
                    stack.push(currentNode.right);
                }
                if (currentNode.left != null) {
                    stack.push(currentNode.left);
                }
            }
            return res;
        }

        public List<Integer> dfs5(TreeNode root) {
            List<Integer> res = new ArrayList<>();
            Stack<TreeNode> stack = new Stack<>();
            stack.push(root);
            while (!stack.isEmpty()) {
                TreeNode currentNode = stack.pop();
                res.add(currentNode.val);
                if (currentNode.right != null) {
                    stack.push(currentNode.right);
                }
                if (currentNode.left != null) {
                    stack.push(currentNode.left);
                }
            }
            return res;
        }

        public List<Integer> dfs6(TreeNode root) {
            List<Integer> res = new ArrayList<>();
            Stack<TreeNode> stack = new Stack<>();
            stack.push(root);
            while (!stack.isEmpty()) {
                TreeNode currentNode = stack.pop();
                res.add(currentNode.val);
                if (currentNode.right != null) {
                    stack.push(currentNode.right);
                }
                if (currentNode.left != null) {
                    stack.push(currentNode.left);
                }
            }
            return res;
        }

        public List<Integer> dfs7(TreeNode root) {
            List<Integer> res = new ArrayList<>();
            Stack<TreeNode> stack = new Stack<>();
            stack.push(root);
            while (!stack.isEmpty()) {
                TreeNode node = stack.pop();
                res.add(node.val);
                if (node.right != null) {
                    stack.push(node.right);
                }
                if (node.left != null) {
                    stack.push(node.left);
                }
            }
            return res;
        }
    }

    class withRecursion {

        public void dfs1(TreeNode root, List<Integer> res) {
            if (null != root) {
                res.add(root.val);
                dfs1(root.left, res);
                dfs1(root.right, res);
            }
        }

        public void dfs2(TreeNode root, List<Integer> res) {
            if (null != root) {
                res.add(root.val);
                dfs2(root.left, res);
                dfs2(root.right, res);
            }
        }

        public void dfs3(TreeNode root, List<Integer> res) {
            if (null != root) {
                res.add(root.val);
                if (null != root.left) {
                    dfs3(root.left, res);
                }
                if (null != root.right) {
                    dfs3(root.right, res);
                }
            }
        }

        public void dfs4(TreeNode root, List<Integer> res) {
            if (root == null) {
                return;
            }
            res.add(root.val);
            if (root.left != null) {
                dfs4(root.left, res);
            }
            if (root.right != null) {
                dfs4(root.right, res);
            }
        }

        public void dfs5(TreeNode root, List<Integer> res) {
            if (root == null) {
                return;
            }
            res.add(root.val);
            if (root.left != null) {
                dfs5(root.left, res);
            }
            if (root.right != null) {
                dfs5(root.right, res);
            }
        }

        public void dfs6(TreeNode root, List<Integer> res) {
            if (root == null) {
                return;
            }
            if (root.left != null) {
                dfs6(root.left, res);
            }
            if (root.right != null) {
                dfs6(root.right, res);
            }
        }
    }


}
